package com.living.filedownloader;

import android.util.SparseArray;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.living.filedownloader.ui.adapter.TaskItemViewHolder;

/**
 * Activity 的绑定器 adapter ViewHodler 更新器
 */
public class GlobalTasksManager {

    static GlobalTasksManager instance;

    public static GlobalTasksManager getInstance() {
        if (instance == null) instance = new GlobalTasksManager();
        return instance;
    }

    private SparseArray<BaseDownloadTask> taskSparseArray = new SparseArray<>();

    /**
     * 加入下载进度监听
     *
     * @param task
     */
    public void addTaskForViewHolder(BaseDownloadTask task) {
        taskSparseArray.put(task.getId(), task);
    }

    public boolean isHasTask(final int taskid) {
        return taskSparseArray.get(taskid) != null;
    }

    public BaseDownloadTask getTask(final int taskid) {
        return taskSparseArray.get(taskid);
    }

    /**
     * 移除下载进度监听
     *
     * @param taskid
     */
    public void removeTaskForViewHolder(final int taskid) {
        taskSparseArray.remove(taskid);
    }

    /***
     * 更新下载进度监听
     * @param taskid
     * @param holder
     */
    public void updateViewHolder(final int taskid, final TaskItemViewHolder holder) {
        final BaseDownloadTask task = taskSparseArray.get(taskid);
        if (task == null) {
            return;
        }
        task.setTag(holder);
    }


}
